package com.jwsoft.manager.core.integration.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bifang.engine.assistant.core.annotation.ApiService;
import com.bifang.engine.assistant.core.annotation.OpApi;
import com.bifang.engine.assistant.core.annotation.SaveOplog;
import com.bifang.engine.assistant.core.enums.AuTypeEnum;
import com.bifang.engine.assistant.core.enums.FuncTypeEnum;
import com.bifang.engine.data.core.util.PagerUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jwsoft.manager.common.enums.HouseholdBdEnum;
import com.jwsoft.manager.core.dao.model.EduHousehold;
import org.springdoc.core.converters.ConverterUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import cn.hutool.core.convert.Convert;
import java.util.List;
import com.bifang.engine.assistant.core.exception.AppException;
import org.springframework.util.ObjectUtils;
import org.springframework.transaction.annotation.Transactional;
import com.bifang.engine.assistant.core.enums.BoolEnum;
import com.jwsoft.manager.common.vo.eduHouseholdBd.EduHouseholdBdVO;
import com.jwsoft.manager.common.vo.eduHouseholdBd.EduHouseholdBdKeyVO;
import com.jwsoft.manager.common.vo.eduHouseholdBd.EduHouseholdBdQueryVO;
import com.jwsoft.manager.core.integration.EduHouseholdBdIntegration;
import com.jwsoft.manager.core.dao.service.EduHouseholdBdService;
import com.jwsoft.manager.core.dao.model.EduHouseholdBd;
/**
 * 学生户籍信息比对表业务实现类
 *
 * @author zhiyuan
 * @since 2023-08-11
 */
@ApiService(funcCode = "eduHouseholdBd", title = "学生户籍信息比对表")
public class EduHouseholdBdIntegrationImpl implements EduHouseholdBdIntegration {

    @Autowired
    public EduHouseholdBdService eduHouseholdBdService;

    @Override
    @OpApi(funcCode = "eduHouseholdBd0001", title = "学生户籍信息比对表分页查询", funcType = FuncTypeEnum.query)
    public PageInfo<EduHouseholdBdVO> getList(EduHouseholdBdQueryVO vo) {
        // 开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取1000条
            vo.setPageNum(1);
            vo.setPageSize(1000);
        }
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        QueryWrapper<EduHouseholdBd> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(!ObjectUtils.isEmpty(vo.getYear()),EduHouseholdBd::getYear,vo.getYear())
                .eq(!ObjectUtils.isEmpty(vo.getBdStatus()),EduHouseholdBd::getBdStatus, vo.getBdStatus())
                .like(!ObjectUtils.isEmpty(vo.getFullName()),EduHouseholdBd::getFullName,vo.getFullName())
                .eq(!ObjectUtils.isEmpty(vo.getIdcard()),EduHouseholdBd::getIdcard,vo.getIdcard());
        List<EduHouseholdBd> list = eduHouseholdBdService.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            // 判断是否为空，为空则返回空参数
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        // 不为空则获取分页结果
        PageInfo<EduHouseholdBd> pageInfo = new PageInfo<>(list);
        // 返回参数需要通过common层的vo实体类，用该方法可以批量转换
        List<EduHouseholdBdVO> resultList = Convert.toList(EduHouseholdBdVO.class, pageInfo.getList());
        // 转义数据字典值
        // dictHelper.valueToName(resultList, Arrays.asList(DemoDictEnum.values()));
        return PagerUtil.parsePagerVo(resultList, pageInfo);
    }

    @Override
    @OpApi(funcCode = "eduHouseholdBd0002", title = "学生户籍信息比对表根据主键查询详情", funcType = FuncTypeEnum.query)
    public EduHouseholdBdVO getById(EduHouseholdBdKeyVO vo) {
        EduHouseholdBd entity = eduHouseholdBdService.getById(vo.getEduHouseholdBdId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("查询不到详细信息!");
        }
        EduHouseholdBdVO result = Convert.convert(EduHouseholdBdVO.class, entity);
        return result;
    }
    /** 保存前统一校验*/
    @Override
    @OpApi(funcCode = "eduHouseholdBd0003", title = "学生户籍信息比对表保存前校验", funcType = FuncTypeEnum.other ,checkPrivilege = BoolEnum.FALSE)
    public void checkSave(EduHouseholdBdVO vo) {
        //主键为空为新增校验

        //主键不为空为修改校验
    }

    @Override
    @OpApi(funcCode = "eduHouseholdBd0004", title = "学生户籍信息比对表新增", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "eduHouseholdBd0004",
            operationName = "学生户籍信息比对表新增",
            dataType = "eduHouseholdBdId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduHouseholdBdVO add(EduHouseholdBdVO vo) {
        //保存前校验
        checkSave(vo);
        //保存数据
        EduHouseholdBd entity = Convert.convert(EduHouseholdBd.class, vo);
        eduHouseholdBdService.save(entity);
        //将主键设置回vo使操作日志可以取到
        vo.setEduHouseholdBdId(entity.getEduHouseholdBdId());
        return vo;
    }

    @Override
    @OpApi(funcCode = "eduHouseholdBd0005", title = "学生户籍信息比对表修改", funcType = FuncTypeEnum.update)
    @SaveOplog(
            operationType = "eduHouseholdBd0005",
            operationName = "学生户籍信息比对表修改",
            dataType = "eduHouseholdBdId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduHouseholdBdVO edit(EduHouseholdBdVO vo) {
        if (ObjectUtils.isEmpty(vo.getEduHouseholdBdId())) {
            throw new AppException("传入需要修改的主键不能为空!");
        }
        EduHouseholdBd oldEntity = eduHouseholdBdService.getById(vo.getEduHouseholdBdId());
        if (ObjectUtils.isEmpty(oldEntity)) {
            throw new AppException("查询不到需要修改的信息!");
        }
        //保存前校验
        checkSave(vo);
        //保存数据
        EduHouseholdBd entity = Convert.convert(EduHouseholdBd.class, vo);
        eduHouseholdBdService.updateById(entity);
        return vo;
    }

    @Override
    @OpApi(funcCode = "eduHouseholdBd0006", title = "学生户籍信息比对表根据主键删除", funcType = FuncTypeEnum.delete)
    @SaveOplog(
            operationType = "eduHouseholdBd0006",
            operationName = "学生户籍信息比对表删除",
            dataType = "eduHouseholdBdId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void del(EduHouseholdBdKeyVO vo) {
        if (ObjectUtils.isEmpty(vo.getEduHouseholdBdId())) {
            throw new AppException("传入需要删除的数据主键不能为空!");
        }
        EduHouseholdBd entity = eduHouseholdBdService.getById(vo.getEduHouseholdBdId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("未找到需要删除的信息!");
        }
        eduHouseholdBdService.removeById(vo.getEduHouseholdBdId());
    }

    /**
     * 调用mybatisPlus方法进行批量更新
     */
    public void batchEdit(List<EduHouseholdBdVO> updatedItems) {
        List<EduHouseholdBd> list = Convert.toList(EduHouseholdBd.class, updatedItems);
        eduHouseholdBdService.updateBatchById(list);
    }
}
